#include"BlockQueue.hpp"
#include"Task.hpp"

// pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;

void* Productor(void* args)
{
    BlockQueue<task>* bq=static_cast<BlockQueue<task>*>(args);

    while(true)
    {
        //消费
        task ct=bq->pop();
        ct.run();

        cout<<"成功解决了一个任务:"<<ct._data1<<" "<<ct._opr<<" "<<ct._data2<<" "<<"="<<" "<<ct.GetResult()<<endl;
    }
}

void* Consumer(void* args)
{
    BlockQueue<task>* bq=static_cast<BlockQueue<task>*>(args);

    while(true)
    {
        //生产
        task pt;
        pt._data1=rand()%10+1;//[1,10]
        pt._data2=rand()%10;//[0,9]
        pt._opr=oprs[rand()%strlen(oprs)];

        bq->push(pt);

        cout<<"成功创建了一个任务："<<pt._data1<<" "<<pt._opr<<" "<<pt._data2<<" "<<"="<<endl;

        usleep(1000*100);
    }
}

int main()
{
    srand(time(nullptr));
    BlockQueue<task>* bq =new BlockQueue<task>();
    pthread_t p,c;

    pthread_create(&p,nullptr,Productor,bq);
    pthread_create(&c,nullptr,Consumer,bq);

    pthread_join(p,nullptr);
    pthread_join(c,nullptr);
    return 0;
}